<?php

namespace App\Http\Model\Api;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Redis;
use App\Http\Model\AudioApi;
use App\Http\Model\Base;
use App\Http\Model\User;
use App\Http\Model\UserM;
use Exception;

class UserConvene extends Base
{
    protected $table = 'user_convene';
    protected $connection = "mysql_kkaudio";
    protected $primaryKey = 'id';

    public function __construct(array $attributes = [])
    {
        parent::__construct($this->table, $attributes);
    }

    public function getUserConveneByInviteUser(int $invite_user_id) {
      return self::with(['user'])->where('invite_user_id', $invite_user_id)->get();
    }

    public function getActiveUserConvene($invite_user_id, $state) {
      return self::with(['user'])
      ->where('invite_user_id', $invite_user_id)
      ->where('state', $state)
      ->get();
    }

    public function invite(){
      return $this->belongsTo(UserM::class, 'invite_user_id', 'user_id');
    }

    public function user() {
      return $this->belongsTo(UserM::class, 'user_id', 'user_id');
    }

    /**
     * 判断是否有邀请者
     *
     * @param integer $user_id
     * @return boolean
     */
    public static function hasInvite(int $user_id) {
      return self::where('user_id', $user_id)->first();
    }
}